package com.qs.home.ui.VM;

import android.app.Application;
import android.databinding.BindingAdapter;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.qs.base.router.RouterActivityPath;
import com.qs.base.utils.PhotoImgUtil;
import com.qs.base.utils.QRUtil;
import com.qs.home.R;

import java.io.File;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 二维码扫描结果vm
 *
 * @Author ltzz
 * @Date 2019/8/23
 */
public class QRResultViewModel extends BaseViewModel {
    public QRResultViewModel(@NonNull Application application) {
        super(application);
    }

    //二维码扫描数据
    public ObservableField<String> qrResult = new ObservableField<>("");
    //二维码生成数据
    public ObservableField<String> qrData = new ObservableField<>("");
    //二维码链接
    public ObservableField<String> qrUrl=new ObservableField<>("");

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //复制文本
        public ObservableBoolean copyTextUc = new ObservableBoolean(false);
    }

    //复制文本
    public BindingCommand textClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.copyTextUc.set(!uc.copyTextUc.get());
        }
    });
    //二维码生成按钮
    public BindingCommand generateClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            ARouter.getInstance().build(RouterActivityPath.Home.QR_GENERATE_RESULT_ACTIVITY).withString("generateResult", qrData.get()).navigation();
        }
    });
    //保存二维码至相册
    public BindingCommand savePhotoClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            showDialog();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Message message = new Message();
                    try {
                        Bitmap bitmap = PhotoImgUtil.returnBitMap(QRUtil.getQrHttp(qrData.get()));
                        message.obj = bitmap;
                        message.what = 0;
                    } catch (Exception e) {
                        message.what = 1;
                    }
                    handler.sendMessage(message);
                }
            }).start();
        }
    });

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            dismissDialog();
            if (msg.what == 0) {
                PhotoImgUtil.saveBitmap(getApplication(), (Bitmap) msg.obj, "qrImage");
                ToastUtils.showShort("保存二维码成功");
            } else
                ToastUtils.showShort("保存二维码失败");
        }
    };


    @BindingAdapter({"imageUrl"})
    public static void loadImage(ImageView imageView, String url) {
        RequestOptions mRequestOptions = new RequestOptions()
                .override(imageView.getWidth(), imageView.getHeight())
                .skipMemoryCache(true) // 不使用内存缓存
                .diskCacheStrategy(DiskCacheStrategy.NONE) // 不使用磁盘缓存
                .error(R.drawable.no_banner);
        Glide.with(imageView.getContext())
                .load(url)
                .apply(mRequestOptions)
                .into(imageView);
    }
}
